package com.gugee.ins.common.dal.mapper.blogger;

import com.gugee.common.mapper.GugeeMapper;
import com.gugee.ins.common.model.mysql.blogger.InsRelBloggerActiveFollower;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface InsRelBloggerActiveFollowerMapper extends GugeeMapper<InsRelBloggerActiveFollower> {



    @Select({"<script>",
            "SELECT count(*) FROM ins_rel_blogger_active_follower ba LEFT JOIN ins_blogger b ON ba.f_uid = b.uid " +
            "WHERE ba.uid = #{uid} ",
            "<when test='null!=beginAge'> ",
            "AND  b.age &gt;= #{beginAge} ",
            "</when>",
            "<when test='null!=endAge'> ",
            "AND  b.age &lt;= #{endAge} ",
            "</when>",
            "</script>"})
    long countAgeRangeByUid(long uid, Integer beginAge, Integer endAge);

    @Select({"<script>","SELECT count(*) " +
            "FROM ins_rel_blogger_active_follower ba LEFT JOIN ins_blogger b ON ba.f_uid = b.uid " +
            "WHERE ba.uid = #{uid} and  b.gender = #{gender} ",
            "<when test='null!=beginAge'> ",
            "AND  b.age &gt;= #{beginAge} ",
            "</when>",
            "<when test='null!=endAge'> ",
            "AND  b.age &lt;= #{endAge} ",
            "</when>",
            "</script>"})
    long countGenderByUid(long uid, Integer gender,Integer beginAge,Integer endAge);

    @Select("select count(*) from ins_rel_blogger_active_follower baf left join ins_blogger b on baf.f_uid = b.uid where baf.uid = #{uid} and b.age > 1")
    int countByUidForAge(long uid);


    @Select("select count(*) from ins_rel_blogger_active_follower baf left join ins_blogger b on baf.f_uid = b.uid where baf.uid = #{uid} and b.language is not null and b.language != 'un'")
    int countByUidForLanguage(long uid);

    @Select("select count(*) from ins_rel_blogger_active_follower baf left join ins_blogger b on baf.f_uid = b.uid where baf.uid = #{uid} and b.nation is not null ")
    int countByUidForNation(long uid);

    /**
     * 根据评论表信息批量插入
     * @param uid
     * @param insCommentCollectActiveFollowerCount
     * @return
     */
    @Insert("INSERT into `ins_rel_blogger_active_follower` (`id`,`uid`,`f_uid`,`create_time`) " +
            "SELECT t2.id,t2.m_uid,t2.c_uid,t2.create_time FROM " +
            "(SELECT max(id) id,m_uid,c_uid,max(create_time)create_time from `ins_comment` t1 where m_uid = #{uid} GROUP BY m_uid,c_uid) t2 " +
            " where not EXISTS(select 1 from `ins_rel_blogger_active_follower` tt where tt.uid = #{uid} and tt.f_uid = t2.c_uid ) ")
    void batchInsertByUid(@Param("uid") long uid,@Param("insCommentCollectActiveFollowerCount")int insCommentCollectActiveFollowerCount);


}